package com.apes.framework.plugin.condition.model;

import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.StringUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * 功能：条件字段- 条件表的关键字段，如客户、物料
 *
 * @author xul
 * @create 2018-11-06 17:17
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ConditionField.CONST_TABLE_NAME)
public class ConditionField implements ApesBean {
    public static final String CONST_TABLE_NAME = "tech_condition_field";

    /** 条件记录参数 **/
    public static final String AMOUNT_FIELD_NAME = "amount"; //金额字段名
    public static final String STEP_FIELD_NAME = "step"; //阶梯基值字段名
    public static final String START_DATE_FIELD_NAME = "startDate"; //开始时间
    public static final String END_DATE_FIELD_NAME = "endDate"; //结束时间
    public static final String UOM_FIELD_NAME = "uom"; //计量单位
    public static final String CURRENCY_FIELD_NAME = "currency"; //币种
    public static final String PER_FIELD_NAME = "per"; //条件单位

    /** 输入参数 **/
    public static final String Qty_FIELD_NAME = "qty"; //入参：数量

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "条件字段")
    @Comment("字段代码")
    private String id;

    @Column(name = "name", nullable = false, length = 120)
    @Comment("字段名称")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type", referencedColumnName = "id")
    @Comment("字段类型")
    private Enumeration type;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "widget_type", referencedColumnName = "id")
    @Comment("控件类型")
    private Enumeration widgetType;

    @Column(name = "api", length = 64)
    @Comment("接口")
    private String api;

    @Column(name = "search_form_id", length = 64)
    @Comment("弹窗表单编码")
    private String searchFormId;

    @Column(name = "value_formatter", length = 64)
    @Comment("显示格式化")
    private String valueFormatter;

    @Column(name = "to_", length = 64)
    @Comment("mapper字段")
    private String to;

    @Transient String required = "nullable = true";
    public String getRequired() {
        return required;
    }

    public void setRequired(String required) {
        this.required = required;
    }

    @Transient
    public void setRequired(boolean required) {
        if (required){
            this.required ="nullable = false";
        }else{
            this.required ="nullable = true";
        }
    }

    @Transient
    public String propertyScript() {
        if ((findFieldClass().indexOf("Date") == -1 && findFieldClass().indexOf("double") == -1) && ApesBean.class.isAssignableFrom(fieldClass())) {
            return String.format("    @ManyToOne(fetch = FetchType.LAZY)\n" +
                                 "    @JoinColumn(name = \"%s\" , referencedColumnName = \"id\" , %s)\n"+
                                 "    private %s %s;\n",
                    columnName(),required, findFieldType(),fieldName());
        }
        if (findFieldClass().indexOf("Date") > -1) {
            return String.format("     @Temporal(TemporalType.DATE)\n"+
                                 "     @Column(name = \"%s\",%s)\n"+
                                 "     private  %s %s;\n",
                    columnName(),required, findFieldType(),fieldName());
        }else if (findFieldClass().indexOf("String") > -1){
            return String.format("    @Column(name = \"%s\",length = 128)\n"+
                            "    private  %s %s;\n",
                    columnName(), findFieldType(),fieldName());
        }
        String order="\n";
        if (fieldName().equals(STEP_FIELD_NAME)){
            order="@OrderBy(\" step asc\")\n";
        }
        return String.format("    %s@Column(name = \"%s\", nullable = false, columnDefinition = \"NUMBER(16,4) DEFAULT 0\")\n"+
                             "    private  %s %s;\n",
                order,columnName(), findFieldType(),fieldName());
    }
    @Transient
    public String methodScript() {
           String  getStr =  String.format("    public %s get%s(){\r\n" +
                                           "      return %s;\r\n"+
                                           "    }\r\n",
                    findFieldType(), StringUtil.toUpperFirst(fieldName()),fieldName());
            String setStr = String.format("    public void set%s(%s %s){\r\n" +
                                          "      this.%s = %s;\r\n"+
                                          "    }\r\n",
                      StringUtil.toUpperFirst(fieldName()), findFieldType(),fieldName(),fieldName(),fieldName());
            return getStr + setStr;
    }

    @Transient
    public String importEntityScript() {
       if (findFieldClass().indexOf("Date") == -1 && findFieldClass().indexOf("double") == -1) {
           return String.format("import %s;\r\n",
                   findFieldClass());
       }
       return "";
    }

    @Transient
    public String fieldName() {
        return id;
    }

    @Transient
    public String columnName() {
        if (findFieldClass().indexOf("double") > -1) return id;
        if (ApesBean.class.isAssignableFrom(fieldClass())) {
            return String.format("%s_id", id);
        }
        return id;
    }

    @Transient
    public String findFieldType() {
        return getType().getCode();
    }


    @Transient
    public String findFieldClass() {
        return getType().getId();
    }

    //@Transient
    public Class fieldClass() {
        Class clazz;
        try {
            clazz = Class.forName(type.getId());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return clazz;
    }
    
    @Transient /*是否主表*/
    private String attribution;

    public String getAttribution() {
        return attribution;
    }

    public void setAttribution(String attribution) {
        this.attribution = attribution;
    }

    @Transient /*执行表主从*/
    private String attributionExec="";

    public String getAttributionExec() {
        return attributionExec;
    }

    public void setAttributionExec(String attributionExec) {
        this.attributionExec = attributionExec;
    }

}
